package ui;

import component.firstPage.Login._Login;
import java.awt.*;
import javax.swing.*;

public class MainFrame extends JFrame {
    private CardLayout cardLayout;
    private JPanel cardPanel;
    private String currentUserId;

    public MainFrame() {
        // 设置窗口属性
        setTitle("小云线上医疗系统");
        setSize(800, 600);
        setLocationRelativeTo(null);  // 窗口居中
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // 使用卡片布局
        cardLayout = new CardLayout();
        cardPanel = new JPanel(cardLayout);
        
        // 添加各个页面
        cardPanel.add(createLoginPanel(), "login");
        cardPanel.add(createMainMenuPanel(), "mainMenu");
        
        add(cardPanel);
        
        // 显示登录页面
        cardLayout.show(cardPanel, "login");
    }

    private JPanel createLoginPanel() {
        JPanel panel = new JPanel() {
            private Image backgroundImage = new ImageIcon(getClass().getResource("/imgLogo.png")).getImage();

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
            }
        };
        panel.setLayout(new GridBagLayout());
        panel.setBackground(new Color(240, 248, 255));  // 设置淡蓝色背景
        
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        
        // 标题
        JLabel titleLabel = new JLabel("小云线上医疗系统");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        panel.add(titleLabel, gbc);
        
        // 账号输入
        JLabel accountLabel = new JLabel("账号：");
        JTextField accountField = new JTextField(20);
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        panel.add(accountLabel, gbc);
        gbc.gridx = 1;
        panel.add(accountField, gbc);
        
        // 密码输入
        JLabel passwordLabel = new JLabel("密码：");
        JPasswordField passwordField = new JPasswordField(20);
        gbc.gridx = 0;
        gbc.gridy = 2;
        panel.add(passwordLabel, gbc);
        gbc.gridx = 1;
        panel.add(passwordField, gbc);
        
        // 按钮面板
        JPanel buttonPanel = new JPanel();
        buttonPanel.setOpaque(false);
        JButton loginButton = new JButton("登录");
        JButton registerButton = new JButton("注册");
        
        // 设置按钮样式
        styleButton(loginButton);
        styleButton(registerButton);
        
        buttonPanel.add(loginButton);
        buttonPanel.add(registerButton);
        
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        panel.add(buttonPanel, gbc);
        
        // 登录按钮事件
        loginButton.addActionListener(e -> {
            String account = accountField.getText();
            String password = new String(passwordField.getPassword());
            
            String userId = _Login.login(account, password);
            if (userId != null) {
                currentUserId = userId;  // 存储用户ID
                cardLayout.show(cardPanel, "mainMenu");
            } else {
                JOptionPane.showMessageDialog(this, "账号或密码错误！");
            }
        });
        
        return panel;
    }

    private JPanel createMainMenuPanel() {
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBackground(new Color(240, 248, 255));
        
        // 顶部标题
        JLabel titleLabel = new JLabel("医疗系统主菜单", SwingConstants.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        titleLabel.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0));
        panel.add(titleLabel, BorderLayout.NORTH);
        
        // 主菜单按钮
        JPanel buttonPanel = new JPanel(new GridLayout(4, 1, 10, 10));
        buttonPanel.setOpaque(false);
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(20, 150, 20, 150));
        
        JButton[] buttons = new JButton[] {
            new JButton("预约挂号"),
            new JButton("查看我的预约"),
            new JButton("查看就医记录"),
            new JButton("退出系统")
        };
        
        for (JButton button : buttons) {
            styleButton(button);
            buttonPanel.add(button);
        }
        
        panel.add(buttonPanel, BorderLayout.CENTER);
        
        // 添加按钮事件
        buttons[0].addActionListener(e -> {
            AppointmentDialog dialog = new AppointmentDialog(this, currentUserId);
            dialog.setVisible(true);
        });
        
        buttons[1].addActionListener(e -> {
            AppointmentListDialog dialog = new AppointmentListDialog(this, currentUserId);
            dialog.setVisible(true);
        });
        
        buttons[2].addActionListener(e -> {
            MedicalRecordDialog dialog = new MedicalRecordDialog(this, currentUserId);
            dialog.setVisible(true);
        });
        
        buttons[3].addActionListener(e -> {
            // 退出系统
            int choice = JOptionPane.showConfirmDialog(this, 
                "确定要退出系统吗？", "退出确认", 
                JOptionPane.YES_NO_OPTION);
            if (choice == JOptionPane.YES_OPTION) {
                System.exit(0);
            }
        });
        
        return panel;
    }
    
    private void styleButton(JButton button) {
        button.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        button.setBackground(new Color(70, 130, 180));
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createRaisedBevelBorder());
        
        // 鼠标悬停效果
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button.setBackground(new Color(100, 149, 237));
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                button.setBackground(new Color(70, 130, 180));
            }
        });
    }
} 